<?php
/**
 * Created by PhpStorm. 微信相关接口
 * User: FDG367
 * Date: 2017/10/10
 * Time: 15:09
 */
namespace common\helps;

use linslin\yii2\curl\Curl;
use Yii;
use common\helps;
use yii\helpers\Url;
class Wechat{

	const WECHAT_CONTENT	= '尊敬的顾客您好，您有%d张优惠券未兑换领取，请在【会员中心-我的优惠券】查看验证码前往线下门店兑换，祝您购物愉快。';
	
	const WECHAT_TEMPLATE = 'a0R41gFW230wW9S9LIk1ufXc8BmYgcx8d1iw_IdCdKE';
	
	const WECHAT_SEND_MSG_COLOR = '#173177';
	
	const WECHAT_SEND_REMARK = '请您于有效期前凭兑换码到店兑换，防止过期无效';
	/**
	 * @param $build拼接url
	 * @return string
	 */
	private static function httpBuildQuery($build){
		return  http_build_query($build,'','&',PHP_QUERY_RFC3986);
	}

	/**
	 * @param 微信扫码登录
	 * @return 微信二维码链接
	 */
	public static function wechatConnectQrconnect(){

		$params	=[
			'appid'			=>Yii::$app->params['wechat_scan_code']['app_id'],
			'redirect_uri'	=>Yii::$app->params['wechat_pc_callback_url'],
			'response_type'	=>'code',
			'scope'			=>'snsapi_login',
			'state'			=>'STATE#wechat_redirect',
		];
		$sortStr	= self::httpBuildQuery($params);

		return Yii::$app->params['wechat_url']['connect_qrconnect'].$sortStr;
	}

	/**
	 * @param 微信授权拼接参数
	 * @param string
	 */
	public static function wechatOauth(){
		$params	=[
			'appid'			=>Yii::$app->params['wechat_authorization']['app_id'],
			'redirect_uri'	=>Yii::$app->params['wechat_callback_url'],
			'response_type'	=>'code',
			'scope'			=>'snsapi_userinfo',
			'state'			=>'STATE#wechat_redirect',
		];
		$sortStr	= self::httpBuildQuery($params);

		return Yii::$app->params['wechat_url']['oauth_authorize'].$sortStr;
	}


	/**
	 * @param 微信获取token 和对应的openid
	 * @param 授权登录token没有限制无需记录数据库
	 * @param object
	 */
	public static function getWechatAccessToken($code,$appid,$secret){

		$params	= [
			'appid'		=>$appid,
			'secret'	=>$secret,
			'code'		=>$code,
			'grant_type'=>'authorization_code',
		];

		$curlModel	= new Curl();
		$curlModel->setOptions([CURLOPT_SSL_VERIFYPEER=>false]);
		$curlModel->setGetParams($params);
		$curlModel->get(Yii::$app->params['wechat_url']['oauth_access_token']);
		return json_decode($curlModel->response);
	}

	/**
	 * @param 获取微信用户的基本信息
	 * @return object
	 */
	public static function getWechatSnsUserinfo($openid,$access_token){

		$params	= [
			'access_token'	=>$access_token,
			'openid'		=>$openid,
		];

		$curlModel	= new Curl();
		$curlModel->setOptions([CURLOPT_SSL_VERIFYPEER=>false]);
		$curlModel->setGetParams($params);
		$curlModel->get(Yii::$app->params['wechat_url']['sns_userinfo']);
		return json_decode($curlModel->response);
	}

	/**
	 * @param 获取wechatToken
	 */
	public static  function getWechatToken(){
		//服务器获取token
		$url = Yii::$app->params['jdstb_shanghaihomeexpo_token'];

		//$url = Yii::$app->params['wechat_url']['access_token']."&appid=".Yii::$app->params['wechat_authorization']['app_id']."&secret=".Yii::$app->params['wechat_authorization']['secret'];
		$curlModel	= new Curl();
		$curlModel->setOptions([CURLOPT_SSL_VERIFYPEER=>false]);
		//$curlModel->setGetParams($params);
		$curlModel->get($url);
		return $curlModel->response;
	}

	/**
	 * @param 获取微信分享的票据
	 */
	public static function getJsApiTicket(){

		//获取微信token
		$accessToken=redisHelper::getWechatToken();

		$url = Yii::$app->params['wechat_url']['jsapi'].$accessToken;

		$curlModel	= new Curl();
		$curlModel->setOptions([CURLOPT_SSL_VERIFYPEER=>false]);
		//$curlModel->setGetParams($params);
		$curlModel->get($url);
		return json_decode($curlModel->response);
	}

	/**
	 * @param array $touser 微信推送消息的openid集合 [openid1,openid2]
	 * @return bool
	 */
	public static function sendWechatMessage($touser=[],$title='优惠券过期提醒',$count=0,$message_data=[]){

		//获取微信token
		//$content	= sprintf(self::WECHAT_CONTENT,$count);
		$accessToken=redisHelper::getWechatToken();
		$url = Yii::$app->params['wechat_url']['sendmessage'].$accessToken;
		//拼装推送参数
		$params	= [
			'touser'      =>$touser,
		    'template_id' =>self::WECHAT_TEMPLATE,
		    'url'         =>Yii::$app->params['wap'].'/customer/coupon',
		    'data'=>[
		        'first'=>[
		            'value'=>$title,
		            'color'=>self::WECHAT_SEND_MSG_COLOR
		        ],
		        'keyword1'=>[
		            'value'=>$message_data['keyword1'],
		            'color'=>self::WECHAT_SEND_MSG_COLOR
		        ],
		        'keyword2'=>[
		            'value'=>$message_data['keyword2'],
		            'color'=>self::WECHAT_SEND_MSG_COLOR
		        ],
		        'keyword3'=>[
		            'value'=>$message_data['keyword3'],
		            'color'=>self::WECHAT_SEND_MSG_COLOR
		        ],
		        'keyword4'=>[
		            'value'=>$message_data['keyword4'],
		            'color'=>self::WECHAT_SEND_MSG_COLOR
		        ],
		       /* 'keyword5'=>[
		            'value'=>$message_data['keyword5'],
		            'color'=>self::WECHAT_SEND_MSG_COLOR
		        ],*/
		        'remark'=>[
		            'value'=>self::WECHAT_SEND_REMARK,
		            'color'=>self::WECHAT_SEND_MSG_COLOR
		        ]
		    ]
		];
		//发送数据
		$curlModel	= new Curl();
		$curlModel->setOptions([CURLOPT_SSL_VERIFYPEER=>false]);
		//$curlModel->setPostParams($params);
		$curlModel->setRequestBody(json_encode($params));
		$curlModel->post($url);

		return $curlModel->response;
	}
	/**
	 * @param 获取微信模板行业
	 */
	public static function getIndustry(){

		//获取微信token
		$accessToken=redisHelper::getWechatToken();

		$url = Yii::$app->params['wechat_url']['industry'].$accessToken;

		$curlModel	= new Curl();
		$curlModel->setOptions([CURLOPT_SSL_VERIFYPEER=>false]);
		//$curlModel->setGetParams($params);
		$curlModel->get($url);
		return json_decode($curlModel->response);
	}

}